<template>
  <div>
    <div class="login_container">
      <div style="width: 400px; height: 460px; margin: 150px auto; background-color:rgba(107,149,224,0.5); border-radius: 10px">
        <div style="width: 100%; height: 100px; font-size: 30px; line-height: 100px; text-align: center; color: #4a5ed0">欢迎登录</div>
        <div style="margin-top: 25px; text-align: center; height: 320px;">
          <el-form :model="users">
            <el-form-item>
              <div>
                <el-radio v-model="users.identity" label="admin" border>管理员</el-radio>
                <el-radio v-model="users.identity" label="家长" border>家长</el-radio>
                <el-radio v-model="users.identity" label="孩子" border>孩子</el-radio>
              </div>
            </el-form-item>
            <el-form-item>
              <el-input v-model="users.userName" prefix-icon="el-icon-user" style="width: 80%" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="users.password" show-password prefix-icon="el-icon-lock" style="width: 80%" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item>
              <div style="display: flex; justify-content: center">
                <el-input v-model="users.verCode" prefix-icon="el-icon-user" style="width: 42%; margin-right: 10px" placeholder="请输入验证码"></el-input>
                <img :src="captchaUrl" @click="clickImg()" width="140px" height="40px" />
              </div>
            </el-form-item>
            <el-form-item>
              <el-button style="width: 80%; margin-top: 10px" type="primary" @click="login()">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  data() {
    return {
      users: {
        userName:'',
        password:'',
        identity:'',
        verCode:'',
      },
      radio:'',
      key: '',
      captchaUrl:'',
    }
  },
  mounted() {
    this.key = Math.random();
        this.captchaUrl = 'http://localhost:8088/api/captcha?key=' + this.key;
  },
  methods: {
    login(){
      request.post("/Users/login?key=" + this.key,this.users).then(res =>{
        if(res.code === '0'){
          this.$message({
            message: '登录成功',
            type: 'success'
          });
          //将登录用户信息存储在localStorage中
          localStorage.setItem("user", JSON.stringify(res.data));
          console.log(res.data);
          if(res.data.identity === 'admin'){
            this.$router.push('/A_UsersManagement');
          }
          if(res.data.identity === '家长'){
            this.$router.push('/A_UsersManagement');
          }
          if(res.data.identity === '孩子'){
            this.$router.push('/Common_Devices');
          }
        }else{
          this.$message({
            message: res.msg,
            type: 'error',
          });
          this.key = Math.random();
          this.captchaUrl = 'http://localhost:8088/api/captcha?key=' + this.key;
          this.users.verCode = '';

        }
      })

    },
    clickImg(){
      this.key = Math.random();
      this.captchaUrl = 'http://localhost:8088/api/captcha?key=' + this.key;
      this.users.verCode = '';
    }
  }
}
</script>

<style scoped>
.login_container{
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/login_bg.jpg");
  background-size: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
